/**
 * Project Name:kany-project-contact
 * File Name:StartUp.java
 * Package Name:me.kany.project.contact.main
 * Date:2016-1-15下午12:40:10
 * Copyright (c) 2016, Jason.Wang All Rights Reserved.
 *
*/

package me.kany.project.contact.main;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

/**
 * ClassName:StartUp <br/>
 * Function: 创建联系人的主要启动函数. <br/>
 * Reason: 当前版本主要是针对集团内部的通讯录编写的，复用率不高. <br/>
 * Date: 2016-1-15 下午12:40:10 <br/>
 * 
 * @author Jason.Wang
 * @version
 * @since JDK 1.7
 * @see
 */
public class StartUp {
	private static POIFSFileSystem fs;
	private static HSSFWorkbook wb;
	private static HSSFSheet sheet;
	private static HSSFRow row;
	private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
	private static Pattern pattern = Pattern.compile("[0-9a-zA-Z\u4E00-\u9FA5]");

	/**
	 * main:程序的主要启动函数. <br/>
	 *
	 * @author Jason.Wang
	 * @param args
	 * @since JDK 1.7
	 */
	public static void main(String[] args) {
		try {
			InputStream inputStream = new FileInputStream("test.xls");
			Map<String, String> content = new HashMap<String, String>();
			fs = new POIFSFileSystem(inputStream);
			wb = new HSSFWorkbook(fs);
			sheet = wb.getSheetAt(0);
			// 得到总行数
			int rowNum = sheet.getLastRowNum();
			row = sheet.getRow(0);
			// 正文内容应该从第二行开始,第一行为表头的标题
			for (int i = 3; i <= rowNum; i++) {
				row = sheet.getRow(i);
				String name1 = getCellFormatValue(row.getCell(3)).trim().replace("      ", "").replaceAll(" ", "");
				String mobile1 = getCellFormatValue(row.getCell(4)).trim().replace("      ", "").replaceAll(" ", "");
				Matcher name = pattern.matcher(name1);
				Matcher mobile = pattern.matcher(mobile1);
				if (name.find() && mobile.find()) {
					content.put(name1, mobile1);
				}

				String name2 = getCellFormatValue(row.getCell(9)).trim().replace("      ", "").replaceAll(" ", "");
				String mobile2 = getCellFormatValue(row.getCell(10)).trim().replace("      ", "").replaceAll(" ", "");
				name = pattern.matcher(name2);
				mobile = pattern.matcher(mobile2);
				if (name.find() && mobile.find()) {
					content.put(name2, mobile2);
				}
			}
			Set<String> keys = content.keySet();
			for (String key : keys) {
				System.out.println(key + "\t" + content.get(key));
			}
			System.out.println(content.size());

		} catch (FileNotFoundException e) {
			System.out.println("未找到指定路径的文件!");
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 根据HSSFCell类型设置数据
	 * 
	 * @param cell
	 * @return
	 */
	private static String getCellFormatValue(HSSFCell cell) {
		String cellvalue = "";
		if (cell != null) {
			// 判断当前Cell的Type
			switch (cell.getCellType()) {
			// 如果当前Cell的Type为NUMERIC
			case HSSFCell.CELL_TYPE_NUMERIC:
			case HSSFCell.CELL_TYPE_FORMULA: {
				// 判断当前的cell是否为Date
				if (HSSFDateUtil.isCellDateFormatted(cell)) {
					// 如果是Date类型则，转化为Data格式
					// 方法1：这样子的data格式是带时分秒的：2011-10-12 0:00:00
					// cellvalue = cell.getDateCellValue().toLocaleString();

					// 方法2：这样子的data格式是不带带时分秒的：2011-10-12
					Date date = cell.getDateCellValue();
					cellvalue = simpleDateFormat.format(date);

				}
				// 如果是纯数字
				else {
					// 取得当前Cell的数值
					cellvalue = String.valueOf(cell.getNumericCellValue());
				}
				break;
			}
				// 如果当前Cell的Type为STRIN
			case HSSFCell.CELL_TYPE_STRING:
				// 取得当前的Cell字符串
				cellvalue = cell.getRichStringCellValue().getString();
				break;
			// 默认的Cell值
			default:
				cellvalue = " ";
			}
		} else {
			cellvalue = "";
		}
		return cellvalue;
	}

}
